import React, { Component } from 'react'
import {ListBox} from '../../../components'
import UpdatePage from './UpdateNew'
import AddPage from './AddPage'

import {fetchData} from '../../../components/AjaxConsumer'
import {  Button, Modal, Form, Input, Radio,Upload, Icon } from 'antd';
export default class ListPurchasePage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableColumn: {},
            searchComponentData: [],
            visible: false,
        };
        //行操作列表
        this.rowOperation = [
            {
                handleName: 'edit',
                iconClass: 'cellEdit'
            }, {
                handleName: 'delete',
                iconClass: 'cellDele'
            }, {
                handleName: 'assignUser',
                iconClass: 'cellRole'
            }
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'add',
                showName: '新增',
                iconClass: 'plus',
                //permission: 'team-add'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit',
                //permission: 'team-edit'
            }
            ,{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete',
                //permission: 'team-del'
            }
            //,{
            //    handleName: 'upload',
            //    showName: '打印',
            //    iconClass: 'upload',
            //    permission: 'rawbarcode-print'
            //}
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'purchaseId',
            data: [
                {
                    showName: 'purchaseId',
                    columnName: 'purchaseId',
                    columnHidden:true,
                    foreverHidden: true
                },{
                    showName: '货主编码',
                    columnName: 'purchaseCode',
                    // columnHidden:true,
                    // foreverHidden: true
                },{
                    showName: '货主名称',
                    columnName: 'purchaseName',
                    columnWidth: 140
                },{
                    showName: '状态',
                    columnName: 'activeFlag',
                    showOption: [//页面显示配置
                        {
                            optionKey: '1',
                            optionShow: '启用'
                        }, {
                            optionKey: '2',
                            optionShow: '禁用'
                        }
                    ],
                    columnWidth: 100
                }, {
                    showName: '创建时间',
                    columnName: 'gmtCreate',
                    columnWidth: 140
                }, {
                    showName: '创建人',
                    columnName: 'createBy',
                    columnWidth: 80
                }
            ]
        };

        //搜索属性配置
        this.searchComponentData = [
            {
                showName: '状态',
                name: 'activeFlag',
                type: 'select',
                optionList: [//下拉框的信息
                //下拉框的信息
                {
                    optionKey: '',
                    optionShow: ''
                }, {
                    optionKey: '1',
                    optionShow: '启用'
                }, {
                    optionKey: '2',
                    optionShow: '禁用'
                }
            ]
            },
            {
                showName:'创建时间',
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin',
                showTime: '1'
            },
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        console.log("strHandles===ListResource=======", strHandles);
        if (strHandles === 'edit') {
            this.editHandle(rowObj);
        } else if (strHandles === 'delete') {
            this.deleteHandle(rowObj);
        } else if (strHandles === 'add') {
            this.addHandle();
        } else if (strHandles == 'upload') {
            this.upload();
        }
    }
    // 增加班组
    addHandle = () => {
        console.log("====执行了新增班组操作====");
        this.refs.addPage.addWorkareaHandle();
    }

    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateWorkarea.editHandle(rowObj[0].data);
        }
    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateWorkarea.deleteHandle(rowObj[0].data)
        }
    }
    // // 给区域分配角色
    // assignFactoryUserHandle = (rowObj) => {
    //     let workareaId = rowObj.workareaId;
    //     this.refs.assignFactoryUser.initModal(workareaId);
    // }


    //刷新列表
    refreshHandle = () => {
        this.refs.listPurchase.refreshHandle();
    }

    // componentWillMount() {
    //     const dicType = 'model_type';
    //     const options= {
    //         url: '/base/dic/get/' + dicType,
    //         type: 'get',
    //         condition:{}
    //     };
    //     //先写死，后面再改
    //     //fetchData(this, options, this.dicInitCallBackHandle);
    // }

    // //下拉框 初始化
    // dicInitCallBackHandle = (resData)=>{
    //     //alert('resData' + JSON.stringify(resData, null, 4));
    //     let optionList = [];
    //     let tmp = {};
    //     tmp.optionKey = '';
    //     tmp.optionShow = '';
    //     optionList.push(tmp);
    //     resData.data.map( (e,index) => {
    //         let tmp = {};
    //         tmp.optionKey = e.dicCode;
    //         tmp.optionShow = e.dicName;
    //         optionList.push(tmp);
    //     });

    //     //alert('optionList' + JSON.stringify(optionList, null, 4));
    //     this.tableColumn.data[3].showOption = optionList;

    //     this.searchComponentData[3].optionList = optionList;
    //     //this.setState({
    //     //    tableColumn: this.tableColumn,
    //     //    searchComponentData: this.searchComponentData
    //     //})

    // }

  handleCancel = () => {
    this.setState({ visible: false });
      this.form.resetFields();
    
  }
  handleCreate = () => {
    const form = this.form;
    form.validateFields((err, values) => {
      if (err) {
        return;
      }

      console.log('Received values of form: ', values);
      form.resetFields();
      this.setState({ visible: false });
    });
  }
  saveFormRef = (form) => {
    this.form = form;
  }


    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return (
            <div>
                <ListBox
                    ref='listPurchase'
                    title="货主详情"
                    isHideLeft={isHideLeft}
                    tableColumnData={this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url='/base/data/purchase/list'
                    rowHandles={this.rowHandles}
                    rowOperation={this.rowOperation}
                    headerOperation={this.headerOperation}
                    isRowSingle = "true"
                />

                <UpdatePage
                    ref="updateWorkarea"
                    refreshHandle={this.refreshHandle}
                />

                <AddPage
                    ref="addPage"
                    refreshHandle={this.refreshHandle}
                />
            </div>
        )
    }
}
